package
{
	import com.util.FpsMeter;
	
	import flash.display.Sprite;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	
	import leaf.science.Global;
	import leaf.science.ui.demos.DemoHotspot;
	import leaf.science.ui.demos.DemosCamera;
	import leaf.science.ui.jogwheel.JogWheel;
	import leaf.science.ui.rightmenu.RightMenu;
	
	[SWF(frameRate="30", backgroundColor="#ECECEC", width="900", height="600")]
	public class ComponentTest extends Sprite
	{
		public var rightMenu:RightMenu;
		public var camera:DemosCamera;
		public var jogWheel:JogWheel;
		
		private var _fps:FpsMeter;
		
		public function ComponentTest()
		{
			stage.scaleMode = StageScaleMode.NO_SCALE;
			
			
			rightMenu = new RightMenu("Choose a demo");
			addChild(rightMenu);
			rightMenu.x = int(stage.stageWidth/2);
			rightMenu.y = int(stage.stageHeight/2);
			
			var data:Array = [];
			for each (var item:XML in Global.DATA_DEMOS.demo){
				data.push({id:uint(item.id), title:item.title.toString()});
			}
			
			rightMenu.addEventListener(Event.CHANGE, onRightSelect);
			rightMenu.data = data;
			
			jogWheel = new JogWheel();
			jogWheel.addEventListener( Event.CHANGE, onJogWheelChange );
			jogWheel.x = int(stage.stageWidth/2);
			jogWheel.y = int(stage.stageHeight/2);
			addChild( jogWheel );
			
			camera = new DemosCamera();
			//camera.scaleX = camera.scaleY = .8;
			camera.addEventListener( Event.CHANGE, onCameraChange );
			camera.addEventListener( MouseEvent.CLICK, onCameraClick );
			camera.x = int(stage.stageWidth/2);
			camera.y = int(stage.stageHeight/2);
			addChild( camera );	
			
			_fps = new FpsMeter();
			addChild( _fps );
			
			stage.addEventListener( Event.ENTER_FRAME, onEnterFrame );
			stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
			
		}
		
		private function onEnterFrame( evt:Event ):void
		{
			_fps.update();
		}
		
		private function onRightSelect (evt:Event):void
		{
			camera.hotspotId = rightMenu.selectedID;
		}
		
		private function onJogWheelChange( evt:Event ):void
		{
			camera.frame = Math.round( camera.totalFrames * jogWheel.value );
		}
		
		private function onCameraChange( evt:Event ):void
		{
			jogWheel.value = camera.angle/360;
		}
		
		private function onCameraClick (evt:MouseEvent):void
		{
			if (evt.target is DemoHotspot)
			rightMenu.selectedID = camera.hotspot.id;
		}
		
		private function onKeyUp (evt:KeyboardEvent):void
		{
			rightMenu.onKeyUp(evt);
		}
		
		
	}
}